package com.haople.sso.core.service.impl;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

import javax.servlet.http.Cookie;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.haople.sso.core.service.CookieService;
import com.haople.sso.core.util.AES;
import com.haople.sso.core.vo.Ticket;

/**
 * cookie 业务
 * @author Administrator
 *
 */
@Service
public class CookieServiceImpl implements CookieService {

	private static final Logger logger=Logger.getLogger(CookieServiceImpl.class);
	private static final String COOKIE_SSO_BASE64="c_sso";
	private static final String COOKIE_SSO_USERKEY_BASE64="c_sso_userkey";
	private static final String COOKIE_SSO_TICKET_BASE64="c_sso_ticket";
	private static final String COOKIE_SSO_CREAREDATE="c_sso_createdate";
	
	@Value("${cookie_secret}")
	private String cookieSecret;
	
	/**
	 * 删除cookie
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public  Cookie delCookies(String domain){
		Cookie cookie=new Cookie(COOKIE_SSO_BASE64, null);
		cookie.setPath("/");
		if(!StringUtils.isEmpty(domain)) {
			cookie.setDomain(domain);
		}
		cookie.setMaxAge(-1);
		return cookie;
	} 
	
	/**
	 * 通过票据获取cookie
	 * @param ticket
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public  Cookie getCookies(Ticket ticket) throws UnsupportedEncodingException{
		JSONObject jsonObject=new JSONObject();
		logger.info("票据信息-------"+ticket);
		jsonObject.put(COOKIE_SSO_USERKEY_BASE64, ticket.getUserId());
		jsonObject.put(COOKIE_SSO_TICKET_BASE64, ticket.getTicket());
		jsonObject.put(COOKIE_SSO_CREAREDATE, ticket.getCreateDate());
		// AES-->base64
		byte[] bytes=AES.encrypt(jsonObject.toJSONString(), cookieSecret);
		String cookieStr = Base64.getEncoder().encodeToString(bytes);
		Cookie cookie=new Cookie(COOKIE_SSO_BASE64, cookieStr);
		cookie.setComment("cookie_sso");
		cookie.setHttpOnly(true);
		cookie.setPath("/");
		return cookie;
	} 
	
	/**
	 * 通过cookie 获取票据
	 * @param cookies
	 * @return
	 */
	public  Ticket getTicket(Cookie[] cookies){
		if(cookies==null){
			return null;
		}
		for(Cookie cookie:cookies){
			try {
				if(COOKIE_SSO_BASE64.equals(cookie.getName())){
					String cookieValue=cookie.getValue();
					//base64-->AES
					byte[] bytes=Base64.getDecoder().decode(cookieValue);
					String ticketStr=AES.decrypt(bytes, cookieSecret);
					JSONObject jsonObject=JSONObject.parseObject(ticketStr);
					Ticket ticket=new Ticket();
					ticket.setCreateDate(jsonObject.getDate(COOKIE_SSO_CREAREDATE));
					ticket.setTicket(jsonObject.getString(COOKIE_SSO_TICKET_BASE64));
					ticket.setUserId(jsonObject.getString(COOKIE_SSO_USERKEY_BASE64));
					return ticket;
				}
			} catch (Exception e) {
				logger.error("---从cookie获取ticket--异常--", e);
			}
		}
		return null;
	}

	
	public void setCookieSecret(String cookieSecret) {
		this.cookieSecret = cookieSecret;
	}
	
	
	
}
